﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Restaurant.DAO
{

    public class ChiTietKhoBD
    {
        private SqlConnection conn = KetNoiDB.TaoKetNoi();

        public DataTable LayChiTietKho( int ma)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spLayChiTietKho";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKho", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayChiTietKhoNL(int maKho, int maNL)
        {
            try
            {
                conn.Open();
                string strCmd = "dbo.spLayChiTietKhoTheoNL";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKho", maKho));
                cmd.Parameters.Add(new SqlParameter("@maNL", maNL));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayNguyenLieuCTKhoNCC(int maKho, int maNCC)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayNguyenLieuCTKhoNCC";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKho", maKho));
                cmd.Parameters.Add(new SqlParameter("@maNCC", maNCC));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayNguyenLieuCTKhoLoaiNL(int maKho, int maLoaiNL)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayNguyenLieuCTKhoLoaiNL";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKho", maKho));
                cmd.Parameters.Add(new SqlParameter("@maLoaiNL", maLoaiNL));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool ThemChiTietKho(int maKho, int maNL, float toiDa, float toiThieu, float Ton, int TTrang)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spThemChiTietKho";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKho", maKho));
                cmd.Parameters.Add(new SqlParameter("@maNL", maNL));
                cmd.Parameters.Add(new SqlParameter("@toiDa", toiDa));
                cmd.Parameters.Add(new SqlParameter("@toiThieu", toiThieu));
                cmd.Parameters.Add(new SqlParameter("@Ton", Ton));
                cmd.Parameters.Add(new SqlParameter("@TTrang", TTrang));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                conn.Close();
                return false;
            }
        }

        public bool XoaChiTietKho(int maKho, int maNL)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spXoaChiTietKho";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKho", maKho));
                cmd.Parameters.Add(new SqlParameter("@maNL", maNL));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                conn.Close();
                return false;
            }
        }

        public bool SuaChiTietKho(int maKho, int maNL, float toiDa, float toiThieu, float Ton, int TTrang)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spSuaChiTietKho";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKho", maKho));
                cmd.Parameters.Add(new SqlParameter("@maNL", maNL));
                cmd.Parameters.Add(new SqlParameter("@toiDa", toiDa));
                cmd.Parameters.Add(new SqlParameter("@toiThieu", toiThieu));
                cmd.Parameters.Add(new SqlParameter("@Ton", Ton));
                cmd.Parameters.Add(new SqlParameter("@TTrang", TTrang));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                conn.Close();
                return false;
            }
        }

        public bool SuaChiTietKhoErro(int maKho, int maNL, float toiDa, float toiThieu, float Ton, int TTrang)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spDeadlock_SuaChiTietKho_Error";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKho", maKho));
                cmd.Parameters.Add(new SqlParameter("@maNL", maNL));
                cmd.Parameters.Add(new SqlParameter("@toiDa", toiDa));
                cmd.Parameters.Add(new SqlParameter("@toiThieu", toiThieu));
                cmd.Parameters.Add(new SqlParameter("@Ton", Ton));
                cmd.Parameters.Add(new SqlParameter("@TTrang", TTrang));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                conn.Close();
                return false;
            }
        }

        public bool SuaChiTietKhoFix(int maKho, int maNL, float toiDa, float toiThieu, float Ton, int TTrang)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spDeadlock_SuaChiTietKho_Fixed";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKho", maKho));
                cmd.Parameters.Add(new SqlParameter("@maNL", maNL));
                cmd.Parameters.Add(new SqlParameter("@toiDa", toiDa));
                cmd.Parameters.Add(new SqlParameter("@toiThieu", toiThieu));
                cmd.Parameters.Add(new SqlParameter("@Ton", Ton));
                cmd.Parameters.Add(new SqlParameter("@TTrang", TTrang));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                conn.Close();
                return false;
            }
        }

        
    }
}
